/*
 * WaveConvert
 * Copyright (c) 2011 Shun Moriya <shun@mnu.sakura.ne.jp>
 *
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#define _CRT_SECURE_NO_WARNINGS

#include "WaveConvert.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char in_filename[_MAX_PATH];
static char out_filename[_MAX_PATH];
static FILE* infile;
static FILE* outfile;

/*
 * check command line
 */
static bool parse_arguments(int argc, char* argv[])
{
	bool is_out_filename = false;

	if(argc == 1)
		return false;

	for(int cmdcnt = 1; cmdcnt < argc; cmdcnt++)
	{
		char* cmdptr = argv[cmdcnt];
		if(*cmdptr == '-')
		{
			cmdptr++;
			switch(toupper((int)*cmdptr))
			{
			case '?':
				return false;

			case 'H':
				return false;

			case 'O':
				if(++cmdcnt >= argc)
				{
					fprintf(stderr, "No output file name\n");
					return false;
				}

				strcpy(out_filename, argv[cmdcnt]);
				is_out_filename = true;
				break;
			}
		}
		else
		{
			strcpy(in_filename, cmdptr);
		}
	}

	return true;
}

////////////////////////////////////////////////////////////////////////////////
// main
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
	char* help =
		"usage:WaveConverter [switch] file\n"
		"            -ofile      output file name\n";
	bool result;

	in_filename[0] = out_filename[0] = '\0';
	infile = outfile = NULL;

	printf("LZ77 sliding dictionary v1.04 by Shun MORIYA\n");
	if(!parse_arguments(argc, argv))
	{
		printf(help);
		return 1;
	}

	if(out_filename[0] == '\0')
	{
		char drive[_MAX_DRIVE];
		char dir[_MAX_DIR];
		char file[_MAX_FNAME];
		char ext[_MAX_EXT];

		_splitpath(in_filename, drive, dir, file, ext);
		strcpy(ext, ".bank");
		_makepath(out_filename, drive, dir, file, ext);
	}

	if(strcmp(in_filename, out_filename) == 0)
	{
		fprintf(stderr, "input and output file name is same.\n", out_filename);
		return 1;
	}
	if((infile = fopen(in_filename, "rb")) == 0)
	{
		fprintf(stderr, "can't open file '%s'\n", in_filename);
		return 1;
	}
	if((outfile = fopen(out_filename, "wb")) == 0)
	{
		fprintf(stderr, "can't open file '%s'\n", out_filename);
		return 1;
	}

	{
		WaveConvert converter;
		WaveConvert::Status status = converter.convert(outfile, infile);
		//converter.dumpwave(infile);
		result = status == WaveConvert::SUCCESS;

	}

	fclose(infile);
	fclose(outfile);

	if(!result)
	{
		remove(out_filename);
	}

	return 0;
}
